package com.ruoyi.project.loan.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.loan.domain.LoanContract;
import com.ruoyi.project.loan.service.ILoanContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 贷款合同Controller
 *
 * @author ruoyi
 * @date 2025-11-11
 */
@RestController
@RequestMapping("/loan/contract" )
public class LoanContractController extends BaseController {
    @Autowired
    private ILoanContractService loanContractService;

    /**
     * 查询贷款合同列表(不分页)
     */
    @PreAuthorize("@ss.hasPermi('loan:contract:list')" )
    @GetMapping("/all/list" )
    public AjaxResult allList(LoanContract loanContract) {
        List<LoanContract> list = loanContractService.selectLoanContractList(loanContract);
        return success(list);
    }

    /**
     * 查询贷款合同列表
     */
    @PreAuthorize("@ss.hasPermi('loan:contract:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(LoanContract loanContract) {
        startPage();
        List<LoanContract> list = loanContractService.selectLoanContractList(loanContract);
        return getDataTable(list);
    }

    /**
     * 导出贷款合同列表
     */
    @PreAuthorize("@ss.hasPermi('loan:contract:export')" )
    @Log(title = "贷款合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, LoanContract loanContract) {
        List<LoanContract> list = loanContractService.selectLoanContractList(loanContract);
        ExcelUtil<LoanContract> util = new ExcelUtil<LoanContract>(LoanContract.class);
        util.exportExcel(response, list, "贷款合同数据" );
    }

    /**
     * 获取贷款合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('loan:contract:query')" )
    @GetMapping(value = "/{contractId}" )
    public AjaxResult getInfo(@PathVariable("contractId" ) Long contractId) {
        return success(loanContractService.selectLoanContractByContractId(contractId));
    }

    /**
     * 新增贷款合同
     */
    @PreAuthorize("@ss.hasPermi('loan:contract:add')" )
    @Log(title = "贷款合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanContract loanContract) {
        return toAjax(loanContractService.insertLoanContract(loanContract));
    }

    /**
     * 修改贷款合同
     */
    @PreAuthorize("@ss.hasPermi('loan:contract:edit')" )
    @Log(title = "贷款合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanContract loanContract) {
        return toAjax(loanContractService.updateLoanContract(loanContract));
    }

    /**
     * 删除贷款合同
     */
    @PreAuthorize("@ss.hasPermi('loan:contract:remove')" )
    @Log(title = "贷款合同", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contractIds}" )
    public AjaxResult remove(@PathVariable Long[] contractIds) {
        return toAjax(loanContractService.deleteLoanContractByContractIds(contractIds));
    }
}
